package pers.xiaojun.boot.tenant.core.util;

import pers.xiaojun.boot.tenant.core.context.TenantContextHolder;

/**
 * 租户工具类
 *
 * @author xiaojun
 * @since 2025-11-23
 */
public class TenantUtils {

    /**
     * 在指定租户下运行逻辑
     * <p>
     * 临时改变当前租户，用于Mybatis-Plus写入时，需要临时改变租户Id的情况
     *
     * @param tenantId 租户Id
     * @param runnable 运行逻辑
     */
    public static void execute(Long tenantId, Runnable runnable) {
        Long oldTenantId = TenantContextHolder.getTenantId();
        try {
            TenantContextHolder.setTenantId(tenantId);
            runnable.run();
        } finally {
            TenantContextHolder.setTenantId(oldTenantId);
        }

    }

}
